package com.zhoug.zmui3demo.ui;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.zhoug.common3.utils.AppUtils;
import com.zhoug.zmui3.ZMUIAnimations;
import com.zhoug.zmui3.ZMUITopView;
import com.zhoug.zmui3.dialog.ZMUIButtonDialog;
import com.zhoug.zmui3.dialog.ZMUIDialog;
import com.zhoug.zmui3.dialog.list.ZMUIListDialog;
import com.zhoug.zmui3demo.R;

import java.util.Arrays;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 测试自定义dialog
 *
 * @Author 35574
 * @Date 2022/4/20
 * @Description
 */
public class TestDialogActivity extends AppCompatActivity {
    private Point mScreenSize;
    private ZMUITopView mTopView;
    private Button mBtnZMUIListDialog;
    private Button mBtnDialog;
    private Button mBtnZMUIDialog;
    private Button mBtnZMUIButtonDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_dialog);
        mScreenSize = AppUtils.getScreenSize();

        findViews();
        bindListener();
    }

    private void findViews() {
        mTopView = findViewById(R.id.topView);
        mBtnZMUIListDialog = findViewById(R.id.btn_ZMUIListDialog);
        mBtnDialog = findViewById(R.id.btn_Dialog);
        mBtnZMUIDialog = findViewById(R.id.btn_ZMUIDialog);
        mBtnZMUIButtonDialog = findViewById(R.id.btn_ZMUIButtonDialog);
    }

    private void bindListener() {
        mBtnZMUIListDialog.setOnClickListener(v -> {
            ZMUIListDialog<String> stringListDialog = new ZMUIListDialog<String>(this) {
                @Override
                public String getText(String itemData) {
                    return itemData;
                }
            };
            stringListDialog.setFullWidth(true);
            stringListDialog.setCorners(60);
            stringListDialog.setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
            stringListDialog.setWindowAnimations(ZMUIAnimations.ANIM_DOWN);
            stringListDialog.setCanceledOnTouchOutside(true);
            stringListDialog.setData(Arrays.asList("北京", "上海", "重庆", "四川", "青海"));
            stringListDialog.setOnItemClickListener(position -> {
                String itemData = stringListDialog.getItemData(position);
                toast(itemData);
            });
            stringListDialog.show();
        });
        mBtnDialog.setOnClickListener(v -> {
            Dialog dialog = new Dialog(this);
            View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_view, null);
            dialog.setContentView(contentView);
            dialog.show();
            Window window = dialog.getWindow();
            if(window!=null){
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
//                attributes.width = mScreenSize.x*3/4;
                attributes.gravity=Gravity.BOTTOM;
                window.setAttributes(attributes);
            }
        });
        mBtnZMUIDialog.setOnClickListener(v->{
            ZMUIDialogImp zmuiDialog=new ZMUIDialogImp(this);

            zmuiDialog.setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
            zmuiDialog.show();
        });

        mBtnZMUIButtonDialog.setOnClickListener(v->{
            ZMUIButtonDialog zmuiButtonDialog=new ZMUIButtonDialog(this);
            zmuiButtonDialog.setCancelable(true);
            zmuiButtonDialog.setCorners(60);
            zmuiButtonDialog.setTitle("标题");
            zmuiButtonDialog.setMessage("几乎是大街上的圣诞节开始对你说的减肥");
            zmuiButtonDialog.setNegativeListener("取消",v1->{
                toast("取消");
                zmuiButtonDialog.dismiss();
            });
            zmuiButtonDialog.setPositiveListener("确定",v1->{
                toast("确定");
                zmuiButtonDialog.dismiss();
            });

            zmuiButtonDialog.show();

        });
    }

    private void toast(String msg) {
        Toast.makeText(this, null != msg ? msg : "", Toast.LENGTH_SHORT).show();
    }

    private static final class ZMUIDialogImp extends ZMUIDialog {

        public ZMUIDialogImp(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getLayoutResId() {
            return R.layout.dialog_content_view;
        }

        @Override
        protected void onViewCreated(Bundle savedInstanceState) {

        }
    }
}
